package com.woniuxy.course.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import com.woniuxy.course.entity.*;
import com.woniuxy.course.exception.GuliException;
import com.woniuxy.course.service.*;
import com.woniuxy.course.utils.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyh
 * @since 2020-07-23
 */
@CrossOrigin //跨域
@RestController
@RequestMapping("/articlerelease")
public class ArticleReleaseController {

    @Autowired
    private ArticleReleaseService articleReleaseService;

    //查询所有文章
    @GetMapping("/findAll")
    @ResponseBody
    public List<ArticleRelease> findAll(){
        List<ArticleRelease> list = articleReleaseService.list(null);
        return list;
    }
    //通过id查询文章
    @RequestMapping("/findArticleById")
    @ResponseBody
    public ArticleRelease findArticleById(Long id){
        System.out.println("查询的id为"+id);
        ArticleRelease articleById = articleReleaseService.findArticleById(id);
        return articleById;
    }
    //通过id更新
    @RequestMapping("/updateArticleById")
    @ResponseBody
    public Integer updateArticleById(ArticleRelease articleRelease){

        Integer articleById = articleReleaseService.updateArticleById(articleRelease);
        return articleById;
    }

    //通过ID逻辑删除
    @RequestMapping("/deleteArticle")
    @ResponseBody
    public Integer removeAriticle(long id){
        System.out.println(id);
        Integer b = articleReleaseService.deleteArticleById(id);

        return b;
    }

    //上传附件
    @PostMapping("'upload")
    public R upload(MultipartFile file){
        String url=null;
        if(null==file){
            throw new GuliException(20001,"传送空文件");
        }else {
          url= articleReleaseService.upload(file);
        }

        if(null==url){
            return R.error().message("文件上传失败,稍后重试");
        }
        return R.ok().data("url",url).message("文件上传成功");
    }
    //上传文章
    @PostMapping("release")
    public R releaseArticle(ArticleRelease articleRelease){
        System.out.println(articleRelease);
        //获取当前老师id
        articleRelease.setTcId(0);
        //发布文章
        Integer flag=articleReleaseService.releaseArticle(articleRelease);

        if(0==flag){
            return R.error().message("发布文章失败,稍后重试");
        }else {
            return R.ok().message("文章发布成功");
        }




    }

    //分页查询所有发布文章
    @GetMapping("pageListAll/{current}/{limit}")
    public R pageListAll(@PathVariable long current,@PathVariable long limit){
        Page<ArticleRelease> articleReleasePage = new Page<>();
        articleReleaseService.page(articleReleasePage,null);
        long total = articleReleasePage.getTotal();
        try {
             System.out.println(1/0);
        }catch (Exception e){
            throw new GuliException(20001,"执行了自定义异常");
        }
        

        List<ArticleRelease> records = articleReleasePage.getRecords();
        return R.ok().data("total",total).data("data",records);
    }

}

